import { THREE } from '@amcax/web-render';
const { Object3D, OrthographicCamera } = THREE;
export class KeepSizeObject3D extends Object3D {
  factor: number;
  modelDxid?: string;

  constructor(
    public camera?: THREE.OrthographicCamera | THREE.PerspectiveCamera,
    public needKeepSize = true,
  ) {
    super();
  }

  private getFactor() {
    if (this.camera == null || !this.needKeepSize) {
      return 1;
    }

    if (this.camera instanceof OrthographicCamera) {
      return (this.camera.top - this.camera.bottom) / this.camera.zoom / 80;
    }

    return 1;
  }

  updateMatrixWorld(force: boolean) {
    const factor = this.getFactor();

    if (this.factor !== factor) {
      this.factor = factor;

      this.scale.set(1, 1, 1).multiplyScalar(factor);
    }

    super.updateMatrixWorld(force);
  }
}
